﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using ProgNetComponents.Validation.Rules;

namespace ProgNetComponents.Validation
{
    /// <summary>
    /// ValidationRuleHelper
    /// </summary>
    public static class ValidationRuleHelper
    {
        //static ValidationRuleHelper()
        //{
        //    RegisterRule<AnyOfColumnsRequiredValidationRule>();
        //    RegisterRule<ColumnDecimalRangeValidationRule>();
        //    RegisterRule<ColumnIntRangeValidationRule>();
        //    RegisterRule<ColumnRequiredValidationRule>();
        //    RegisterRule<ColumnStringLengthValidationRule>();
        //    RegisterRule<ColumnUniqueValidationRule>();
        //    RegisterRule<CustomValidationRule>();
        //    RegisterRule<MultiColumnsRequiredValidationRule>();
        //    RegisterRule<MultiColumnsUniqueValidationRule>();
        //    RegisterRule<SetOfColumnsColumnsUniqueValidationRule>();
        //}

        /// <summary>
        /// _Rules
        /// </summary>
        private static Dictionary<string, Type> _Rules = new Dictionary<string, Type>();

        /// <summary>
        /// UserRulesRegistered
        /// </summary>
        private static bool UserRulesRegistered = false;

        /// <summary>
        /// Registers the rule.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">The name.</param>
        public static void RegisterRule<T>(string name) where T : ValidationRule
        {
            _Rules[name] = typeof(T);
        }
        /// <summary>
        /// Registers the rule.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void RegisterRule<T>() where T : ValidationRule
        {
            string name = typeof(T).Name.Replace("ValidationRule", "");
            RegisterRule<T>(name);
        }
        /// <summary>
        /// Registers the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        private static void Register(Type t)
        {
            if (t.IsAbstract)
                return;
            string name = t.Name.Replace("ValidationRule", "");
            _Rules[name] = t;
        }

        /// <summary>
        /// Gets the rules.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <returns></returns>
        public static Dictionary<string, Type> GetRules(System.ComponentModel.Design.IDesignerHost host)
        {
            if (UserRulesRegistered == false)
            {
                Type[] types = host.GetTypes(typeof(ValidationRule));
                foreach (var t in types)
                {
                    Register(t);
                }
            }
            Dictionary<string, Type> newDic = new Dictionary<string, Type>();
            foreach (KeyValuePair<string, Type> kvp in _Rules)
                newDic.Add(kvp.Key, kvp.Value);
            return newDic;
        }

        /// <summary>
        /// Creates the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="host">The host.</param>
        /// <returns></returns>
        public static ValidationRule Create(string name, System.ComponentModel.Design.IDesignerHost host)
        {
            ValidationRule r = null;
            if (name.IsNotNull() & _Rules.ContainsKey(name))
            {
                try
                {
                    r = host.CreateComponent(_Rules[name]) as ValidationRule;
                }
                catch { }
            }
            return r;
        }
    }

}
